package com.csu.jpetstore_reconfiguration_back.Controller;

import com.csu.jpetstore_reconfiguration_back.common.CommonResponse;
import com.csu.jpetstore_reconfiguration_back.domain.Item;
import com.csu.jpetstore_reconfiguration_back.service.ItemService;
import com.csu.jpetstore_reconfiguration_back.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ItemController {
    @Autowired
    ItemService itemService;

    //获取所有item
    @GetMapping("/items")
    @ResponseBody
    public CommonResponse<List<ItemVO>> getAllItems() {
        return itemService.getAllItems();
    }

    //获取单个item
    @GetMapping("/items/{id}")
    @ResponseBody
    public CommonResponse<ItemVO> getItem(@PathVariable("id") String itemId) {
        return itemService.getItem(itemId);
    }

    //根据productId获取item
    @GetMapping("/products/{id}/items")
    @ResponseBody CommonResponse<List<ItemVO>> getItemListByProductId(@PathVariable("id") String productId) {
        return itemService.getItemListByProductId(productId);
    }


    //添加item
    @PostMapping("/items")
    @ResponseBody
    public CommonResponse<ItemVO> addItem(@Validated @RequestBody Item item) {
        return itemService.addItem(item);
    }

    //更新item
    @PostMapping("/items/{id}")
    @ResponseBody
    public CommonResponse<Item> updateItem(@PathVariable("id") String itemId,@Validated @RequestBody Item item) {
        return itemService.updateItem(itemId, item);
    }

    //删除item
    @PostMapping("/items/{id}/delete")
    @ResponseBody
    public CommonResponse<Item> deleteItem(@PathVariable("id") String itemId) {
        return itemService.deleteItem(itemId);
    }
}
